home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / library / hack99 / nmap.stealth.wrapper.htm < prev    next >
Encoding:
Internet Message Format  |  1999-04-28  |  5.0 KB

  1. Date: Mon, 12 Apr 1999 12:43:55 -0500
  2. From: HD Moore <nlog@ings.com>
  3. To: nmap-hackers@insecure.org
  4. Subject: nwrap -- nmap stealth wrapper
  5. Parts/Attachments:
  6.    1 Shown     34 lines  Text
  7.    2 Shown    171 lines  Text
  8. ----------------------------------------
  9.  
  10. I started working on some scripts to 'wrap' nmap and allow for
  11. stealthier scanning routines.  The goals for this script include:
  12.  
  13.         Creating a host/port table and then randomizing it.
  14.         Scanning each host/port combination in a random sequence.
  15.         Easy creation of decoy addresses.
  16.         Parallel scanning with child process management.
  17.         Consolidation of log files into a nlog-style db or MySQL.
  18.  
  19. There are still a number of issues I am working on, if you have any
  20. suggestions/complaints email me:
  21.  
  22. Delay between scans should be a random number within a user-defined range.
  23.  
  24. Decoy addresses should remain the same during each scan to eliminate chance 
  25. of detection by coordinating traffic logs from each scanned host and finding
  26. the real address in each.
  27.  
  28. Log file consolidation (maybe use -m - and read it all from an open pipe?)
  29.  
  30. Better option set for the nwrap script.
  31.  
  32. Attached is the protoype perl script, I wanted to get some feedback
  33. about what stealth options/techniques people wanted to see implemented
  34. in a nmap wrapper script.
  35.  
  36.  
  37. -HD aka spinux
  38.  
  39. http://nlog.ings.com
  40. http://www.trinux.org
  41.     [ Part 2: "Attached Text" ]
  42.  
  43. #!/usr/bin/perl
  44.  
  45. use Getopt::Long;
  46.  
  47. sub exitclean {
  48.    my ($msg) = @_;
  49.    print "$msg\n";
  50.    exit 2;
  51. }
  52.  
  53.  
  54. $SIG{INT}=\&sig_catch;
  55.  
  56. &GetOptions("debug", \$OPTdebug,
  57.             "p:s", \$OPTports,
  58.             "i:s", \$OPTinput);
  59.  
  60. open (INPUT,"<".$OPTinput) || exitclean("Could not open host input file:  $!");
  61. @targets = (<INPUT>);
  62. close(INPUT) || debugprint("close() failed on INPUT: $!");
  63.  
  64.  
  65. # create a host/port list and shuffle it
  66.  
  67. @targets = shuffle(\@targets);
  68. @ports = parse_ports($OPTports);
  69. @ports = shuffle(\@ports);
  70.  
  71. foreach $host (@targets)
  72. {
  73.     chomp($host);
  74.     @ports = shuffle(\@ports);
  75.     foreach $port (@ports)
  76.     {
  77.         push @output, "$host $port";
  78.     }
  79. }
  80. @output = shuffle(\@output);
  81.  
  82.  
  83. # now do something with that host/port list
  84. foreach $out (@output)
  85. {
  86.  ($nmaptarget,$nmapport) = split(/\s+/,$out);
  87.  $logfile = "$nmaptarget.$nmapport.log";
  88.  print "Scanning port $nmapport on $nmaptarget...\n"; 
  89.  system ("nmap -sS -m $logfile -P0 $nmaptarget -p$nmapport -D" . rdecoys(getpppip())) 
  90.  || print "Could not launch nmap:  $!\n";
  91.  
  92. }
  93.  
  94. exit(0);
  95.  
  96.  
  97. #
  98. # Functions
  99. #
  100.  
  101. sub getpppip {
  102.     my $DATA=`ifconfig | grep P-t-P | awk \'\{ print \$2 \}\'`;
  103.     my $crap;
  104.     my $ip;
  105.     chomp($DATA);
  106.     ($crap,$ip) = split(/\:/,$DATA);
  107.     return $ip;
  108. }
  109.  
  110. sub rdecoys {
  111.     my ($ip) = @_;
  112.     my @octets = split(/\./,$ip);
  113.     my $count;
  114.     my @decoys = ();
  115.     my $decoy;
  116.     my $output;
  117.     
  118.     for ($count = 0; $count < 6 ; $count++)
  119.     { $decoys[$count] = int(rand()*255); }
  120.  
  121.     foreach $decoy (@decoys)
  122.     { 
  123.         $output .= "$octets[0].$octets[1].$octets[2].$decoy,";
  124.     }
  125.     $output .="ME";
  126.     return $output;
  127. }
  128.  
  129. sub debugprint {
  130.   ($msg) = @_;
  131.   print "[debug]  $msg\n" unless (!$OPTdebug);
  132. }
  133.  
  134. sub sig_catch {
  135.    my $signame = shift;
  136.    print "\nRecieved SIG$signame, exiting...\n";
  137.    exit 2;
  138. }
  139.  
  140.  
  141. ###############################################################################
  142. #
  143. #   Function:   shuffle
  144. #   Purpose:    Randomize an array
  145. #   To-Do:      Done
  146. #   Date: 04/09/99
  147. #
  148. #   Comments:   This routine was pretty much ripped from 'Perl Cookbook' pg 121-122
  149. #               
  150. ###############################################################################
  151.  
  152.  
  153. sub shuffle {
  154.     my $array = shift;
  155.     my $i = scalar(@$array);
  156.     my $j;
  157.     foreach $item (@$array )
  158.     {
  159.         --$i;
  160.         $j = int rand ($i+1);
  161.         next if $i == $j;
  162.         @$array [$i,$j] = @$array[$j,$i];
  163.     }
  164.     return @$array;
  165. }
  166.  
  167.  
  168.  
  169. ###############################################################################
  170. #
  171. #   Function:   parse_ports
  172. #   Purpose:    Take in an nmap style port list and return an array
  173. #   To-Do:      Add a check to make sure all the ports added are numeric
  174. #   Date: 04/09/99
  175. #
  176. ###############################################################################
  177.  
  178. sub parse_ports {
  179.     my ($portstring) = @_;
  180.     my $splitter = ",";
  181.     my @portlist = ();
  182.     my @portsplit = ();
  183.     my $port;
  184.    
  185.     @portsplit = split($splitter,$portstring);
  186.     foreach $port (@portsplit)
  187.    
  188.     {
  189.         @range = split(/\-/,$port);
  190.         if (scalar(@range) > 1)
  191.         {
  192.             if ($range[0] > $range[1] || $range[0] < 0 || $range[0] > 65535 || $range[1] < 0 || $range[1] > 65535)
  193.             {
  194.                 print "Your range of $range[0] -> $range[1] is invalid!\n";
  195.                 exit(1);
  196.             }
  197.             for ($i = $range[0]; $i < $range[1] + 1; $i++)
  198.             {
  199.                 if ($i > 0 && $i < 65536)
  200.                 {
  201.                     push @portlist, $i;   
  202.                 }
  203.             }
  204.         
  205.         } else {
  206.             push @portlist, $port;
  207.         }
  208.     }
  209.     
  210.     return @portlist;
  211. }
  212.  
  213.